package com.andcard.action;

import java.io.File;
import java.util.LinkedList;

import com.andcard.AndCard;
import com.andcard.R;
import com.andcard.sql.DbAdapter;
import com.andcard.util.IoUtil;
import com.andcard.util.Reference;

import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class TabConfig extends AbsAction implements OnClickListener{

	private EditText entryPathDatabase;
	private EditText entryPathImportFile;
	private EditText entrySeparator;
	
	private Button btCreateDatabase;
	private Button btLoadDatabase;
	private Button btLoadImportFile;
	
	private CheckBox checkReverse;
	private CheckBox checkReadOnly;
	private CheckBox checkAll;
	
	public TabConfig(AndCard activity, DbAdapter dbAdapter){
		
		super(activity, dbAdapter);
		
		Log.d(Reference.TAG, "TabConfig.Constructor");
		
		// EditText
		this.entryPathDatabase = (EditText)activity.findViewById(R.id.entryPathDb);
		this.entryPathImportFile = (EditText)activity.findViewById(R.id.entryPathImportFile);
		this.entrySeparator = (EditText)activity.findViewById(R.id.entrySeparator);
		
		// Buttons
		btCreateDatabase = (Button)activity.findViewById(R.id.btCreateDatabase); 
		btLoadDatabase = (Button)activity.findViewById(R.id.btLoadDatabase);
		btLoadImportFile = (Button)activity.findViewById(R.id.btLoadImportFile);
		btCreateDatabase.setOnClickListener(this);
		btLoadDatabase.setOnClickListener(this);
		btLoadImportFile.setOnClickListener(this);
		
		// CheckButton
		checkReverse = (CheckBox)activity.findViewById(R.id.checkReverse);
		checkReadOnly = (CheckBox)activity.findViewById(R.id.checkReadOnly);
		checkAll = (CheckBox)activity.findViewById(R.id.checkAll);
	}
	
	public void onClick(View v) {
		
		Log.d(Reference.TAG, "TabConfig.onClick - " + v.toString() );
		
		if(v.equals(btCreateDatabase)){
			createDatabase();
		}else if(v.equals(btLoadImportFile)){
			loadImportFile();
		}else if(v.equals(btLoadDatabase)){
			loadDatabase();
		}
	}
	
	// create database
	public void createDatabase(){

		Log.d(Reference.TAG, "TabConfig.createDatabase" );
		
		// no check flagged on create database
		if (this.checkAll.isChecked() || this.checkReadOnly.isChecked() || this.checkReverse.isChecked()){
			this.getActivity().showDialog(Reference.ALERT_NO_CHECK_ON_CREATE);
			return;
		}
		
		if (entryPathDatabase.getText() == null || entryPathDatabase.getText().toString().length() == 0){
			this.getActivity().showDialog(Reference.ALERT_NO_DATABASE_PATH);
			return;
		}
		
		// check file existence
		File dbFile = new File(entryPathDatabase.getText().toString());

		if (dbFile.exists()){
			this.getActivity().showDialog(Reference.ALERT_PATH_DATABASE_EXISTS);
			return;
		}
		
		if (this.getDbAdapter().createDatabase(entryPathDatabase.getText().toString())){
			this.getActivity().showDialog(Reference.ALERT_DATABASE_CREATED);
		}else{
			this.getActivity().showDialog(Reference.ALERT_NO_DATABASE_CREATED);
		}

	}
	
	// load database
	public void loadDatabase(){
		
		Log.d(Reference.TAG, "TabConfig.loadDatabase" );
		
		// some checks
		if (entryPathDatabase.getText() == null || entryPathDatabase.getText().toString().length() == 0){
			this.getActivity().showDialog(Reference.ALERT_NO_DATABASE_PATH);
			return;
		}
		
		// valid path?
		File dbFile = new File(entryPathDatabase.getText().toString());
		
		if (dbFile.exists() == false || dbFile.isDirectory()){
			this.getActivity().showDialog(Reference.ALERT_NO_VALID_DATABASE_PATH);
			return;
		}
		
		// all card option in read only mode
		if (checkAll.isChecked() && checkReadOnly.isChecked() == false){
			this.getActivity().showDialog(Reference.ALERT_ALL_CARD_IN_READ_ONLY_MODE);
			return;
		}
		
		if (this.getDbAdapter().loadDatabase(entryPathDatabase.getText().toString(), checkReverse.isChecked(), checkReadOnly.isChecked(), checkAll.isChecked()) != null){
			this.getActivity().showDialog(Reference.ALERT_DATABASE_LOADED);
			this.getActivity().getTabCard().disableAllNotStart();
			this.getActivity().getTabCard().setCounter(-1);
			
		}else{
			this.getActivity().showDialog(Reference.ALERT_NO_DATABASE_LOADED);
		}	
	}
	
	// load import file
	public void loadImportFile(){
		
		Log.d(Reference.TAG, "TabConfig.loadImportFile" );
	
		// some checks
		if (this.getDbAdapter().isDatabaseOpen() == false){
			this.getActivity().showDialog(Reference.ALERT_NO_DATABASE_OPEN);
			return;
		}else if(this.getDbAdapter().isDatabaseReadOnly()){
			this.getActivity().showDialog(Reference.ALERT_DATABASE_READ_ONLY);
			return;
		}else if (entryPathImportFile.getText() == null || entryPathImportFile.getText().toString().equals("")){
			this.getActivity().showDialog(Reference.ALERT_NO_IMPORT_FILE);
			return;
		}
		
		File importFile = new File(entryPathImportFile.getText().toString());
		if (importFile.canRead() == false){
			this.getActivity().showDialog(Reference.ALERT_NO_VALID_IMPORT_FILE);
			return;
		}
		
		// check separator
		if (entrySeparator.getText() == null || entrySeparator.getText().toString().equals("")){
			this.getActivity().showDialog(Reference.ALERT_NO_VALID_SEPARATOR);
			return;
		}
		
		LinkedList<String[]> listCard = IoUtil.importFile(importFile, entrySeparator.getText().toString());
		
		if (listCard == null || listCard.size() == 0){
			this.getActivity().showDialog(Reference.ALERT_NO_IMPORT);
			return;
		}
		
		int ok = 0;
		int ko = 0;
		
		for (int x = 0; x < listCard.size(); x++){
			if (listCard.get(x) == null || listCard.get(x).length != 2){
				Log.d(Reference.TAG, "TabConfig.loadImportFile - card id " + x + " discarded");
				ko++;
			}else{
				boolean ret = this.getDbAdapter().insertCard(listCard.get(x)[0], listCard.get(x)[1]); 
				if (ret){
					ok++;
				}else{
					ko++;
				}
			}
		}
	}	
}
